import shutil
import sys
import time

import facade
from bs4 import BeautifulSoup
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(TopPath, "download", "cdxg_ams", "download", "issue")
sPath2 = BaseDir.get_new_path(TopPath, "download", "cdxg_ams", "download", "issue2")
BaseDir.create_dir(sPath2)
ListSqls = []
nCount = 0
ListFile = []
configfile = BaseDir.get_new_path(curPath, "db.ini")
logger = facade.get_streamlogger()
mysqlutils = facade.MysqlUtiles(configfile, "db", logger)


# 转移字符
def sqliteEscape(keyWord):
    keyWord = keyWord.replace('\\', '\\\\')
    keyWord = keyWord.replace("'", "''")
    # keyWord = keyWord.replace("[", "/[")
    # keyWord = keyWord.replace("]", "/]")
    # keyWord = keyWord.replace("%", "/%")
    # keyWord = keyWord.replace("&", "/&")
    # keyWord = keyWord.replace("_", "/_")
    # keyWord = keyWord.replace("(", "/(")
    # keyWord = keyWord.replace(")", "/)")
    return keyWord



def SelectListFromDB():
    # conn = MajorDbConnect()
    sSql = "SELECT b.filename FROM ams_issue JOIN (SELECT filename,count(*) as count FROM `ams_issue` GROUP BY `filename`) as b on b.filename=ams_issue.filename WHERE b.count=ams_issue.num GROUP BY b.filename"
    # rows = SelctSqlFromDB(sSql, conn)
    rows = mysqlutils.SelectFromDB(sSql)
    return rows


# 插入数据库
def InsertIntoDbFromList():
    global ListSqls
    # 数据库连接
    mysqlutils.ExeSqlListToDB(ListSqls)
    ListSqls = list()


def GetFileName(filePath):
    global ListSqls
    filename = BaseDir.get_filename_not_extsep(filePath)
    if filename in ListFile:
        print("文件已解析")
        return
    soup = BeautifulSoup(open(filePath, 'r', encoding='utf-8'), "lxml")
    form_tag = soup.find("form", attrs={'name': 'frmAbs'})
    if not form_tag:
        print("没有这个form，请检查")
        # todo zhe li zuo luoji bi ru tongji chuxian qingkuang
        return
    tale_tag = form_tag.find_all('table', border="0", width="100%", class_="articleEntry")
    num = len(tale_tag)
    print(num)
    count = 0
    for table in tale_tag:
        count += 1
        print(count)
        title_tag = table.find("span", class_="hlFld-Title")
        if title_tag:
            title = title_tag.get_text().strip()
            title = sqliteEscape(title)
        else:
            print("观察是否具有标题")
            time.sleep(10)
            title = ''
        page_tag = table.find("span", class_="page fLpage")
        if page_tag:
            page = page_tag.get_text().strip()
        else:
            page = ''
        date_tag = table.find("span", class_="dateLabel")
        if date_tag:
            date = date_tag.get_text().strip()
            date = date.replace("Published online on", "").replace("Posted online on", "").replace(".", "").strip()
            if date == '':
                timestring = ''
            else:
                try:
                    datestruct = time.strptime(date, '%d %b %Y')
                except:
                    try:
                        datestruct = time.strptime(date, '%d %B %Y')
                    except:
                        datestruct = time.strptime(date, '%d %B, %Y')
                timestring = time.strftime("%Y%m%d", datestruct)
        else:
            timestring = ''
        a_tag = table.find("a", class_="ref nowrap abs")
        if a_tag:
            url = a_tag['href']
            print(url)
        else:
            print("url不存在请检查")
            sys.exit(-1)
        sql = "INSERT IGNORE INTO ams_issue(`filename`, `title`,`page`,`datestring`,`num`,`url`) VALUES('{filename}', '{title}','{page}','{datestring}','{num}','{url}') ".format(
            filename=filename, title=title, page=page, datestring=timestring, num=num, url=url)
        print(sql)
        ListSqls.append(sql)
    if len(ListSqls) >= 100:
        InsertIntoDbFromList()


def main():
    filenum = 0
    # 查询数据库中哪些文件存在(方便第一次使用 后面更新时不建议使用 因为这样有些文件就不会解析)
    # rows = SelectListFromDB()
    # for item in rows:
    #     ListFile.append(item[0])
    for filePath in BaseDir.get_dir_all_files(sPath):
        print(filePath)
        # AnalysisFile(filePath,fileName)
        GetFileName(filePath)
        soursepath = BaseDir.get_new_path(sPath2,BaseDir.get_file_name(filePath))
        shutil.move(filePath, soursepath)
        filenum += 1
        print("完成解析文件数量{}".format(filenum))
    InsertIntoDbFromList()


if __name__ == "__main__":
    main()
